class Api::V1::AuthenticationsController < Api::V1::BaseController
  #验证登录，若帐号密码正确，返回jwt token
  #若登录失败，返回错误信息
  def authenticate_user
    user = User.find_by_username(create_params[:username])
    if user.authenticate(create_params[:password])
      render json: payload(user), status: 200
    else
      render json: {errors: ['Invalid Username/Password']}, status: 401
    end
  end

  def pat
    puts 123
    params.require(:user).each do |data|
      puts data[:name]
      puts data[:age]
    end

  end

  private

  def payload(user)
    return nil unless user and user.id
    {
      authen_token: JsonWebToken.encode({user_id: user.id})
    }
  end

  #健壮参数
  def create_params
    params.require(:user).permit(:username,:password)
  end

end
